Tiven Wang
Wang Tiven August 08, 2017
425 favorite favorites
bookmark bookmark
share share

下載本篇完整代碼 Github

Create Service Registry

cf marketplace -s p-service-registry

Create a service registry service:

cf create-service p-service-registry standard service-registry

Show created service:

$ cf service service-registry

Service instance: service-registry
Service: p-service-registry
Bound apps: try-cf-service-registry
Tags:
Plan: standard
Description: Service Registry for Spring Cloud Applications
Documentation url: http://docs.pivotal.io/spring-cloud-services/
Dashboard: https://spring-cloud-service-broker.cfapps.io/dashboard/p-service-registry/377790cc-d543-4a45-8982-66b34c1e8adf

Last Operation
Status: create succeeded
Message:
Started: 2017-08-08T09:42:02Z
Updated: 2017-08-08T09:45:19Z

Rebuild the Applications

To work with Spring Cloud Services service instances, your client applicaiton must include the spring-cloud-services-dependencies and spring-cloud-dependencies BOMs. Unless you are using the spring-boot-starter-parent or spring-cloud-starter-parent or Spring Boot Gradle plugin, you must also specify the spring-boot-dependencies BOM as a dependency.

<dependencyManagement>
  <dependencies>
    <dependency>
        <groupId>io.pivotal.spring.cloud</groupId>
        <artifactId>spring-cloud-services-dependencies</artifactId>
        <version>1.3.1.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Change the spring cloud eureka dependency to

<dependency>
  <groupId>io.pivotal.spring.cloud</groupId>
  <artifactId>spring-cloud-services-starter-service-registry</artifactId>
</dependency>

Spring Cloud Connectors

To connect client applications to the Service Registry, Spring Cloud Services uses Spring Cloud Connectors, including the Spring Cloud Cloud Foundry Connector, which discovers services bound to applications running in Cloud Foundry.

Register a Service

Change the @EnableEurekaClient to @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

The application’s Eureka instance name (the name by which it will be registered in Eureka) will be derived from the value of the spring.application.name property on the application. If you do not provide a value for this property, the application’s Eureka instance name will be derived from its Cloud Foundry application name, as set in manifest.yml:

---
instances: 1
memory: 1G
applications:
  - name: hero-service
  ...

Set the spring.application.name property in application.yml:

spring:
  application:
    name: hero-service

Eureka Instance Auto Configuration

Application use class EurekaInstanceAutoConfiguration to configure a Eureka instance’s settings based on the value of spring.cloud.services.registrationMethod.

  • route will register vcap.application.uris[0]
  • direct will register using the container IP address and PORT environment variable.

The default behaviour is route.

If you want change the default value, you may set the spring.cloud.services.registrationMethod to direct, please refer to Using Container-to-Container Networking:

spring:
  cloud:
    services:
      registrationMethod: direct

Disable HTTP Basic Authentication

The Spring Cloud Services Starter for Service Registry has a dependency on Spring Security. Unless your application has other security configuration, this will cause all application endpoints to be protected by HTTP Basic authentication.

If you do not yet want to address application security, you may turn off Basic authentication by setting the security.basic.enabled property to false. In application.yml or bootstrap.yml.

You might make this setting specific to a profile (such as the dev profile if you want Basic authentication disabled only for development):

---

spring:
  profiles: dev

security:
  basic:
    enabled: false

For more information, see “Security” in the Spring Boot Reference Guide.

Testing

http://police-service-dative-urnfield.cfapps.io/Gotham-City/villains/xman

{
    "name": "xman",
    "id": 1,
    "catched": true
}

Using Container-to-Container Networking

To use Cloud Foundry’s container networking to reach an application registered with the Service Registry, you must add a network policy. You can do this using the Networking cf CLI plugin.

Install Networking cf CLI plugin

Ensure you have a recent version of the CF CLI cf version, Should show version 6.28 or higher. If not, update your CF CLI.

Install the plugin from the Cloud Foundry Community Plugins Repository:

cf install-plugin -r CF-Community network-policy

Setting List Access

Run the cf list-access command to list current network policies:

$ cf list-access
Listing policies as anypossible.w@gmail.com...
OK

Source  Destination     Protocol        Port

Use the cf allow-access command to grant access from the Villain application to the Police application:

$ cf allow-access villain-service police-service --protocol tcp --port 8080
Allowing traffic from villain-service to police-service as anypossible.w@gmail.com...
OK

Use cf list-access again to view the new access policy:

The Villain application can now use container networking to access the Police application via the Service Registry. For more information about configuring container networking, see the Administering Container-to-Container Networking topic in the Pivotal Cloud Foundry documentation.

Self-Signed SSL Certificate

// TODO

References

Similar Posts

  • Try Cloud Foundry 13 - Logs Loggregator is the next generation system for aggregating and streaming logs and metrics from all of the user apps and system components in an Elastic Runtime deployment. Loggregator uses Google's protocol buffers along with gRPC to deliver logs.
  • Try Cloud Foundry 12 - Config Server Config Server for Pivotal Cloud Foundry (PCF) is an externalized application configuration service, which gives you a central place to manage an application’s external properties across all environments.
  • Microservices - API Gateway Implement an API gateway that is the single entry point for all clients. The API gateway handles requests in one of two ways. Some requests are simply proxied/routed to the appropriate service. It handles other requests by fanning out to multiple services.
  • Microservices - Circuit Breaker 访问远程服务时, 比依赖超时时间更好一些的方式是一种叫断路器(Circuit Breaker)的模式. Circuit Breaker 就像一位交通警察, 在前方道路畅通的情况下, 他会放行; 当前方道路由于各种原因拥堵时, 他会告诉你前方道路不通请回; 如果他是个更智能的交警的话, 还会告诉你前方道路部分拥堵, 只允许部分车辆通过, 比如实行单双号.
  • Microservices - Architecture Microservice architecture (MSA) is an approach to building software systems that decomposes business domain models into smaller, consistent, bounded-contexts implemented by services. These services are isolated and autonomous yet communicate to provide some piece of business functionality. Microservices are typically implemented and operated by small teams with enough autonomy that each team and service can change its internal implementation details (including replacing it outright!) with minimal impact across the rest of the system.
  • Try Cloud Foundry 11 - Autoscaler Autoscaler

Comments

comments powered by Disqus
Back to Top