Foreword
SpringCloud micro-service leaders, the best floor plan.
Eureka as a registration center, is one of the most important SpringCloud system core components.
Feign annotated using the interface invoke the service, with the Eureka can achieve load balancing.
Source
GitHub
surroundings
JDK 1.8.0 +
Maven 3.0 +
SpringBoot 2.0.3
SpringCloud Finchley.RELEASE
development tools
IntelliJ IDEA
text
commons project
commons project - POM file
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-eureka-commons
1.0
springcloud-eureka-commons
Public works
jar
UTF-8
1.8
Cairo-SR3
Finchley.RELEASE
io.spring.platform
platform-bom
${platform-bom.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud-dependencies.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
Some common configuration dependent
commons project - Project Structure
service project
There are four modules in this project: a registration center, a two providers and consumers
registry-service (Registry)
registry-service - POM file
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-eureka-service
1.0
com.zwc
springcloud-eureka-registry-service
0.0.1-SNAPSHOT
springcloud-eureka-registry-service
Registry
jar
com.zwc
springcloud-eureka-commons
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-maven-plugin
Mainly added spring-cloud-starter-netflix-eureka-server-dependent
registry-service - application.yml profile
# Port
server:
port: 8761
# Application Name
spring:
application:
name: eurka-server
eureka:
instance:
# Ip instead of using the instance name
prefer-ip-address: true
Examples of the hostname #
hostname: ${spring.cloud.client.ip-address}
Rule # instance ID
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
# Whether to register themselves to the registration center
registerWithEureka: false
# Whether to obtain registration information to the registry
fetchRegistry: false
ServiceUrl:
# Registration Center Address
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
This uses the default port of 8761, of course, can change, but the discovery of registered address of the calling service center side of the port to be consistent with it
registry-service - start class
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaRegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaRegistryServiceApplication.class, args);
}
}
Add @EnableEurekaServer annotation startup class indicates that this project is a registered center
registry-service - to start the project
Visit http 1. Start Project Success: // localhost: 8761 / to see the eureka-server main page
Provider (provider)
Provider - POM file
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-eureka-providerfirst-service
1.0
com.zwc
springcloud-eureka-providerfirst-service-core
1.0
springcloud-eureka-providerfirst-service-core
One provider of engineering services - Core
jar
com.zwc
springcloud-eureka-commons
1.0
com.zwc
springcloud-eureka-providerfirst-service-api
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-maven-plugin
Mainly added spring-cloud-starter-netflix-eureka-client-dependent
Provider - application.yml profile
# Port
server:
port: 8090
# Application Name
spring:
application:
name: say-hello
eureka:
instance:
# Ip instead of using the instance name
prefer-ip-address: true
Examples of the hostname #
hostname: ${spring.cloud.client.ip-address}
Rule # instance ID
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
ServiceUrl:
# Registration Center Address
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
Note here configure registry address port port above 8761 is configured registry project
There are two consumer project, only port inconsistent here, where the port is 8090, and another port for 8091. Will not go
Two consumer engineering role is to achieve load balancing effect
spring.application.name: application name, consumers need to use is called
Provider - controller distal Controller
package com.zwc.providerfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName SayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/15 15:28
* @Version 1.0
*/
@RestController
public class SayHelloController {
/*
* @ClassName SayHelloController
* @Desc TODO reads the configuration file port
* @Date 2019/5/15 15:49
* @Version 1.0
*/
@Value("${server.port}")
private String port;
/*
* @ClassName SayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/15 15:30
* @Version 1.0
*/
@RequestMapping("/hello")
public String hello(){
return "Hello Spring Cloud!!!port:" + port;
}
}
Provide a service: Hello and output ports
Provider - start class
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringcloudEurekaProviderfirstServiceCoreApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaProviderfirstServiceCoreApplication.class, args);
}
}
Add @EnableEurekaClient comment indicates that this project can provide service to the registry
Provider - start the project
Visit http 1. Start Project Success: // localhost: 8090 / hello see the output content 'Hello Spring Cloud !!! port: 8090'
2. Refresh http: // localhost: 8761 / (registry) can see the services have been registered came
3. Similarly, there is a provider of port works just inconsistent, but also start up
Visit http 4. After the project started successfully: // localhost: 8091 / hello see the output content 'Hello Spring Cloud !!! port: 8091'
5. Refresh again http: // localhost: 8761 / (registry) can see the same service provider there are two
Consumer (consumer)
Consumer - POM file
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zwc
springcloud-eureka-consumer-service
1.0
com.zwc
springcloud-eureka-consumer-service-core
1.0
springcloud-eureka-consumer-service-core
Consumer Services Project - Core
jar
com.zwc
springcloud-eureka-commons
1.0
com.zwc
springcloud-eureka-consumer-service-api
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-maven-plugin
Consistent with the provider, the need for spring-cloud-starter-netflix-eureka-client-dependent
Also you need to add Feign start dependent spring-cloud-starter-openfeign
Consumer - application.yml profile
# Port
server:
port: 8080
# Application Name
spring:
application:
name: service-feign
eureka:
instance:
# Ip instead of using the instance name
prefer-ip-address: true
Examples of the hostname #
hostname: ${spring.cloud.client.ip-address}
Rule # instance ID
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client: Wuxi gynecological hospital http://www.ytsg120.cn/
ServiceUrl:
# Registration Center Address
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
Note here configure registry address port port above 8761 is configured registry project
spring.application.name: application name, need to use is called the consumer, it can also be consumed at the same time consumption
Consumer - Service call
package com.zwc.consumer.api.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @ClassName FeignApi
* @Desc TODO using Feign call Api - Interface
* @Date 2019/5/15 16:11
* @Version 1.0
*/
@FeignClient("say-hello")
FeignApi public interface {
/*
* @ClassName FeignApi
* @Desc TODO call / hello method say-hello service name
* @Date 2019/5/15 16:17
* @Version 1.0
*/
@RequestMapping("/hello")
String hello();
}
Which services through @FeignClient ( "say-hello") annotation to specify the call
say-hello is a provider of spring.application.name: Application Name
String hello () ;: can be found, this method is to provide a method by SayHelloController in, only to be defined here as the interface
Note that the provider has to return the same value, the same method and the same parameter name
Consumer - controller distal Controller
package com.zwc.consumer.controller;
import com.zwc.consumer.api.feign.FeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @ClassName FeignController
* @Desc TODO using Feign call Api - Front Controller
* @Date 2019/5/15 16:18
* @Version 1.0
*/
@RestController
public class FeignController {
@Autowired(required = false)
private FeignApi feignApi;
/*
* @ClassName FeignController
* @Desc TODO method invocation Say Hello
* @Date 2019/5/15 16:20
* @Version 1.0
*/
@RequestMapping("/feign")
public String feign(){
return feignApi.hello();
}
}
Use @Autowired comment assembly Bean, Bean calls the service by this method in
Such exposure to the external interface call is actually a service provider
Consumer - start class
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringcloudEurekaConsumerServiceCoreApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaConsumerServiceCoreApplication.class, args);
}
}
Add @EnableEurekaClient comment indicates that this project can provide service to the registry
Add @EnableFeignClients notes indicate on Feign function for remote calls
Consumer - start the project
1. After the success of the project started several visits to http: // localhost: 8080 / feign
2. You can find alternate output 'Hello Spring Cloud !!! port: 8090' and 'Hello Spring Cloud !!! port: 8091'
3. At this point has been reached load balancing effect
4. Refresh again http: // localhost: 8761 / (registry) can be seen at this time more than a consumer
service project - Project Structure
The multi-use project IntelliJ IDEA Open
The project from GitHub downloaded to your local
Open IntelliJ IDEA
Click File -> Open
Open your downloaded to the local project directory
springcloud-eureka -> springcloud-eureka-service (select to open this project)
After opening the service project
Click again File -> Project Structrue
Select Modules, click on the '+' sign
Click Import Module
Or open your downloaded to the local project directory
springcloud-eureka -> springcloud-eureka-commons -> pom.xml
Click OK
Click Next, Finish
Click Apply, OK
Spread
CentOS7 used Docker simple deployment SpringCloud project