Consul is different from Eureka
www.jianshu.com/p/8494698d0…
Consul download
Consul Download address
Consul agent command
-
-data-dir Specifies the directory where the agent stores the data status
-
-config-dir Specifies the location of the service configuration file and check definition
-
-config-file Specifies a configuration file to be loaded
-
-dev Creates a server node in the development environment
-
-bootstrap-expect notifies consulserver of the number of servers to which we are now ready to join. This parameter delays the start of log replication until the specified number of servers have been successfully added
-
-node Specifies the name of a node in the cluster
-
-bind: specifies the IP address of a node
-
– Server Indicates that the node is a Server. You are advised to set the number of servers in each DATA center (DC) to at least 1 and at most 5. All servers use the Raft consistency algorithm to ensure the consistency and linearity of transactions
-
-client Specifies the node as client and the binding address of the client interface, including HTTP, DNS, and RPC
-
-join Adds the node to the cluster
-
-datacenter specifies the datacenter to which the machine is added
-
File Properties
Consul started
Stand-alone mode
consul agent -dev
Cluster mode (three service nodes and one client node)
1. Create a node folder
2. Create basic.json files in each folder
consul-service-1
{
"bind_addr": "127.0.0.1"."data_dir": "./data"."bootstrap_expect": 2."server": true."ports": {
"server": 9300,
"serf_lan": 9301,
"serf_wan": 9302,
"http": 9500,
"dns": 9600}} bind_addr Specifies the node IP address data_dir Specifies the consul service data store location bootstrap_expect declares that the server is started after several Consul services join the cluster Whether to start a port as a service portCopy the code
consul-service-2
{
"bind_addr": "127.0.0.1"."data_dir": "./data"."bootstrap_expect": 2."retry_join": ["127.0.0.1:9301"]."server": true."ports": {
"server": 8300,
"serf_lan": 8301,
"serf_wan": 8302,
"http": 8500,
"dns": 8600}} retry_JOIN Joins the cluster and retries automatically if the join failsCopy the code
consul-service-4
{
"bind_addr": "127.0.0.1"."data_dir": "./data"."bootstrap_expect": 2."retry_join": ["127.0.0.1:9301"]."server": true."ports": {
"server": 6300,
"serf_lan": 6301,
"serf_wan": 6302,
"http": 6500,
"dns": 6600}}Copy the code
consul-client-3
{
"bind_addr": "127.0.0.1"."data_dir": "./data"."retry_join": ["127.0.0.1:9301"]."ports": {
"server": 7300,
"serf_lan": 7301,
"serf_wan": 7302,
"http": 7500,
"dns": 7600}}Copy the code
3. Create the configuration file corresponding to the node number and start the node in sequence
Consul – service – 1 command:
consul agent -config-dir ./ -node consul-service-1 -ui
Copy the code
Consul – service – 2 orders:
consul agent -config-dir ./ -node consul-service-2 -ui
Copy the code
Consul – service – 4 commands:
consul agent -config-dir ./ -node consul-service-4 -ui
Copy the code
Consul – the client – 3 orders:
consul agent -config-dir ./ -node consul-client-3 -ui
Copy the code
-
-node Indicates the name of a node in the cluster
-
-config-dir Indicates the path of the configuration folder
-
If the following command is displayed on the console after two or more nodes are added to the cluster, the leader node is successfully elected and the Consul cluster is accessible
-
Enter Consul members to view cluster status
- Or log in to the Consul UI of any node to view the node
Spring Cloud integrates Consul and makes service calls using OpenFeign
- Relationship between Spring Cloud and Spring Boot Versions Hoxton is used for Demo
1. Pom files
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <! - consul registry - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <! --feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Copy the code
2. Yml files
spring:
application:
name: spring-cloud-consul
profiles:
active: dev
cloud:
consul:
host: localhost
port: 9500
# Start a health check
discovery:
# Heartbeat detection address
healthCheckPath: ${server.servlet.context-path}/actuator/health
# polling time
healthCheckInterval: 15s
server:
port: 8082
servlet:
context-path: /spring-cloud-consul
Copy the code
3, Application start class
- Add annotations
@EnableFeignClients
@EnableDiscoveryClient
Copy the code
4. Use OpenFeign for service invocation
Because you don’t want to write two service provider consumers in the same project, just start two service instances to test
1. Service provider code
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @desc: service provider * @author: xupy * @create: 2020-03-08 20:14 **/ @RestController public class ProvideController { @Value("${server.port}")
private String port;
@GetMapping("hello1")
public String hello1() {return this.port+":hello1"; }}Copy the code
2. Service consumer code
controller
import com.yxy.springcloudconsul.service.OpenFeignService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @desc: consumer * @author: xupy * @create: 2020-03-08 20:26 **/ @RestController public class ConsumerController { @Autowired private OpenFeignService openFeignService; @GetMapping("consumerHello1")
public String consumerHello1() {returnopenFeignService.hello1(); }}Copy the code
service
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; //value= service registration name, path= prefix @feignClient (value ="spring-cloud-consul",path = "${server.servlet.context-path}")
public interface OpenFeignService {
@GetMapping("/hello1")
String hello1();
@GetMapping("/hello2")
String hello2();
@GetMapping("/hello3")
String hello3();
}
Copy the code
3. Start two service instance ports 8081 and 8082 respectively
-
You can see from the console that the two services have been registered
-
Visit http://localhost:8081/spring-cloud-consul/consumerHello1 and frequent refresh can see load balancing two polling services respectively
Spring Cloud uses Consul as a configuration hub (alternative to Spring Cloud Config solution)
1. Pom files are added
<! - consul configuration center - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency>Copy the code
2. Change the yML file to bootstrap.yml and add it
Application. Yml is different from bootstrap.yml
consul:
config:
# Open file configuration center
enabled: true
Configure the folder prefix
prefix: config
Configure the default folder
defaultContext: ${spring.application.name}
Configure the default key
data-key: data
# config file environment separator
profileSeparator: ', '
There may be a conflict in the EG: 1.8 JDK that requires an update
format: YAML
Refresh the configuration periodically
watch:
delay: 1000
enabled: true
Copy the code
3. Create data in Consul
key = prefix+”/”+defaultContext+profileSeparator+active+”/”+data = config/spring-cloud-consul,dev/data
4. Complete code for consumers
import com.yxy.springcloudconsul.service.OpenFeignService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @desc: consumer * @author: xupy * @create: 2020-03-08 20:26 **/ @restController // Config file modified automatically refresh @refreshScope public class ConsumerController {// Configure data in Consul @Value("${author}")
private String author;
@Autowired
private OpenFeignService openFeignService;
@GetMapping("consumerHello1")
public String consumerHello1() {return openFeignService.hello1();
}
@GetMapping("getAuthor")
public String getAuthor() {returnthis.author; }}Copy the code
5. Access service output configuration
Go to http://localhost:8081/spring-cloud-consul/getAuthor
Output:
- Modify consul configuration information
- Once again, visit http://localhost:8081/spring-cloud-consul/getAuthor
Output: