The first set up a springcloud-- producer and consumer Eureka

SpringCloud is an ordered collection of a series of frames. It uses the development of convenience SpringBoot cleverly simplify the development of distributed systems infrastructure, such as service discovery registration, distribution center, message bus, load balancing, circuit breakers, monitoring and other data, can be done with the development of a style SpringBoot key and start the deployment. SpringCloud not reinventing the wheel, it just will present each company developed more mature, and can withstand the test of actual service framework combined, re-packaged masked a complex configuration and implementation of the principles by SpringBoot style, and ultimately to the developers leaving a set of easy to understand, easy to deploy and easy maintenance of distributed systems development kit.
 

Create two maven project, a provider, a consumer.

The path between the two is (only show one consumer item structure):

Springboot first create two projects, one is the default port of 8080, a manual setting 8081 (set in Application.yml in) is.

Pom file which reads:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>consumer</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>consumer</name>
  <description>consumer</description>
  <!-- 只复制下面的内容即可 -->
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

The entrance springboot in Application.java, used to start the project. Says:

package consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

The above is the same as the content of the two projects, followed by the contents of a different file for each project.

The first is the content providerdemo.java inside:

@RestController
public class providerdemo {

	@RequestMapping("provider/demo")
	public String demo() {
		return "provider success!";
	}
}

Next came the contents consumerdemo.java inside:

@RestController
public class consumerdemo {
/*
 * 当我们从服务消费端去调用服务提供者的服务的时候,使用了一个很好用的对象,叫做RestTemplate,
 * 当时我们只使用了RestTemplate中最简单的一个功能getForEntity发起了一个get请求去调用服务端的数据
 */
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("consumer/demo")
	public String consumerdemo() {
		return this.restTemplate.getForObject("http://localhost:8080/provider/demo",String.class);
		
	}
	
}

RestTemplates in SpringCloud, consumers access through this class producer, @ bean annotation to instantiate this class is instantiated by @AutoWired after the introduction of notes, give it to Spring to manage.

Were started two categories, namely access localhost: 8080 / provider / demo and http: // localhost: 8081 / consumer / demo should be able to get this result provider success, indicating a successful call to the consumer in the method provider!.

Question: 
This is the interface after encoding (http:: // localhost 8080 / provider / demo) hard-coded in the code, but the project release, ip address change is inevitable. Moreover, the hard-coded certainly can not achieve load balancing, that is, if start multiple service provider, this is not hard-coded to call the service according to the load balancing policy.

Solution: 
ZooKeeper used in Dubbo as a service registered with the container found in Springcloud is used as a container Eureka service registration and discovery.

Next, add a Eureka project, create a new project, pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Eureka</groupId>
  <artifactId>Eureka</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Eureka</name>
  <description>Eureka</description>
  <!-- 只复制下面的内容即可 -->
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Then add @EnableEurekaServer or @EnableDiscoveryClient on startup class Springboot, the difference of the two annotated notes are:

@EnableEurekaServer is based on spring-cloud-netflix dependent, is only eureka effect, is dedicated to the use of Eureka 
@EnableDiscoveryClient is based on spring-cloud-commons-dependent, and is implemented in the classpath, it is for example zookeeper, consul used, 

@SpringBootApplication
@EnableEurekaServer
public class Application {

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

Note that: SpringApplication.run (Application.class, args); .class inside the class name to be consistent with the class name, is self-starting.

Next, modify the file yml

server: 
  port: 8083
eureka: 
  client: 
    register-with-eureka: false
    fetch-registry: false
    service-url: 
      defaultZone: http://localhost:8082/eureka/

register-with-eureka: false The default value is true, the setting does not have much impact on the use of is true, but at startup will ensure the following error: 
WAS Unable to Refresh the ITS Cache = Status of Can not! execute request on any known server 
because the start time to register itself due to the conflict themselves

defaultZone configure the address of eureka.

If you run the following error problems:

java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V

 

Since spring boot version compatibility is caused, modify the configuration file in pom.xml, before the amendment:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
</parent>

Modified:

 

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.3.RELEASE</version>
       <relativePath/> 
</parent>

Start the project again, a success. (Here I just want to say mmp, changed several versions, each version change time to wait for half an hour maven download, start up and gave me a lot of wrong report, and finally found me just write port 8082 turned out to be repeated, and finally the port can be changed to 8083 successfully launched)

Start successfully enter  HTTP: // localhost: 8083 / (according to its own port) you can see the eureka interface.

Then we need to provider and consumer are placed in the registry, then we need to change some pom file

Both pom same file, only one column:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springcloud</groupId>
  <artifactId>springcloud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springcloud</name>
  <description>springcloud</description>
  
<!-- 只复制下面的内容即可 -->
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>


    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

Change the version number of the parent, increasing the spring-cloud-starter-netflix-eureka-client.

Here to emphasize the relationship of dependency and dependencyManagement

1. dependencyManagement Maven in fact equivalent to a role of their dependency jar package version management manager.

2.pom.xml file, two ways to determine the version of the jar

(1): If the dependencies in the dependency he did not declare version element, then maven

It will go down dependencyManagement which have not been declared to the version artifactId and groupId, if so, to inherit it, if

The error will not tell you have a version for the dependency declaration

(2): If the dependencies of dependency declared version, regardless of the presence or absence dependencyManagement version of the statement of the jar, they are subject to dependency in the version.

Question: If the version inconsistencies such errors occur.

JavaWeb:java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider

To maintain a consistent version of springboot and springcloud.

Back to the topic, to continue to go, and then change the provider and consumer of startup items, add items on the boot class @EnableEurekaClientnotes

Change the provider and consumer of yml file

server: 
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka/
spring:
  application:
    name: provider-demo

Add the eureka registry file in yml address defaultZone: http: // localhost: 8083 / eureka /

If they do not give it to set name, the name of the application is UNKNOWN.

The consumer yml file empathy.

The last first start eureka, then start provider, consumer, can see in the registry:

Thus, the first springcloud to build over, a classic consumer plus producer registry eureka, this part have any questions you can leave a message, I was just beginning to learn, can help as much as possible to help.

Guess you like

Origin blog.csdn.net/qq_39404258/article/details/89552548