Dry Goods sharing microservices Spring-cloud (8. Service Governance and Configuration center Spring-cloud-zooke)

Source: Internet
Author: User
Tags zookeeper

8.1.    Service Governance

Spring-cloud-zookeeperthe modes provided include service discovery and configuration, and configuring dynamic updates does not require manual requests/refreshendpoint. The project is automatically configured and bound to theSpringEnvironment and otherSpringprogramming model idioms, forSpring BootApplication providesZookeeperintegration. With a few simple annotations, you can quickly enable and configure common patterns in your application and useZookeepercomponents to build large distributed systems. Feign,Turbine,Ribbonand theZuulboth withSpring Cloud Zookeepercooperation.

Create a project demo-springcloud-zookeeper, add a primary dependency Spring-cloud-starter-zookeeper-config

Enable the Service governance feature, add primary dependency Spring-cloud-starter-zookeeper-discovery enable the Configuration Center feature. Incidentally, adding dependent org.springframework.boot:spring-boot-configuration-processor is to

enabled Type-safe Configuration Properties

new Startup Class Zookeeperapplication,@EnableDiscoveryClient Enable service registration and discovery,@EnableFeignClients enable Feignclients, note @FeignClient ("Demo-springcloud-zookeeper") here the name to be with spring.application.name is the same as the service name.

@Configuration
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class Zookeeperapplication {

@Value ("${spring.application.name}")
Private String AppName;

@Autowired
Private Loadbalancerclient LoadBalancer;

@Autowired
Private discoveryclient discovery;

@Autowired
Private environment env;

@Autowired
Private Appclient appclient;


@Autowired
Private Ownerproperties ownerproperties;

@RequestMapping ("/")
Public serviceinstance lb () {
Return This.loadBalancer.choose (This.appname);
}

@RequestMapping ("/hi")
Public String hi () {
Return "Hello world! From "+ this.discovery.getLocalServiceInstance ();
}

@RequestMapping ("/self")
Public String self () {
return This.appClient.hi ();
}

@RequestMapping ("/getfromenv")
public string getfromenv (@RequestParam ("prop") string prop) {
return new Relaxedpropertyresolver (this.env). GetProperty (prop, "not Found");
}

@RequestMapping ("/getfromarchaius")
public string Getfromarchaius (@RequestParam ("prop") string prop) {
Return Dynamicpropertyfactory.getinstance (). Getstringproperty (prop, "null"). get ();
}

@RequestMapping ("/getfromconfigproperties")
Public String getfromconfigproperties () {
return Ownerproperties.getfirstname ();
}

@FeignClient ("Demo-springcloud-zookeeper")
Interface Appclient {
@RequestMapping (Path = "/hi", method = Requestmethod.get)
String hi ();
}

@Autowired
Resttemplate rest;

Public String RT () {
Return This.rest.getForObject ("http://" + This.appname + "/hi", String.class);
}

public static void Main (string[] args) {
Springapplication.run (Zookeeperapplication.class, args);
}

@Bean
@LoadBalanced
Resttemplate Loadbalancedresttemplate () {
return new Resttemplate ();
}
}

configuration file application.properties configuration spring.cloud.zookeeper.discovery.register Open the registration service, spring.cloud.zookeeper.config.enabled Open Configuration service,spring.cloud.zookeeper.connectString

Configure the connection ZK address

Start zookeeper, launch the app, service node in zookeeper after successful registration, get the node to see if data validation successfully registered service

Browser Access http://localhost:8080/self interface, which accesses the binding's own provided rest Interface Service through the feign client appclient/hi

8.2.    Configuration Center

Zookeeperprovides aHierarchical Namespaces, allowing clients to store arbitrary data, such as configuration data. Spring Cloud Zookeeper Configis a Config Server and Client alternative to the project. In a special“Boot”stage, the configuration is loaded into theSpringenvironment. By default, the configuration is stored in the/confignamespace. Based on the name of the application and the impersonation parsing properties of theSpring Cloud Configsequence of active profiles, creating multiplePropertysourceinstance. For example, the name"TESTAPP"the application and"Dev"The configuration file creates the following property source:

Config/testapp,dev
Config/testapp
Config/application,dev
Config/application

Properties Yes Config/application namespace for use with zookeeper Properties

Create the ZK node /config and configure the child node /test and set the value to "point and borrow" and the child node person.first-name (Person.firstname, Person.first_name, Person_first_name can also ) and set the value to "point finance"

Browser Access http://localhost:8080/getFromEnv?prop=test

Browser Access http://localhost:8080/getFromConfigProperties

Dry Goods sharing microservices Spring-cloud (8. Service Governance and Configuration center Spring-cloud-zooke)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.